package com.reform.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reform.config.JwtTokenUtil;
import com.reform.pojo.common.Department;
import com.reform.mapper.DepartmentMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IAdminService;
import com.reform.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.service.INumProjectService;
import com.reform.service.SchoolDBService;
import com.reform.utils.RedisUtils;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学院表 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-06-27
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    SchoolDBService schoolDBService;

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    INumProjectService numProjectService;

    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @Autowired
    IAdminService adminService;

    @Autowired
    private RedisUtils redisUtils;

    @Value("${jwt.expireTime}")
    Long expireTime;


    private static int count = 0;

    /**
     * 获取全校部门信息
     *
     * @return
     * @throws SQLException
     */

    @Override
    public Result getSchoolDepartments() throws SQLException {
        List<Department> allDepartments;
        allDepartments = (List<Department>) redisUtils.get("departmentList");
        if (!VerifycUtils.verifyNullOrEmptyUtils(allDepartments) || count != allDepartments.size()) {
            allDepartments = schoolDBService.getAllDepartments();
            count = allDepartments.size();
            redisUtils.set("departmentList", allDepartments, 60 * 60 * 24);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("departments", allDepartments);
        map.put("count", allDepartments.size());
        return Result.success(ResultCode.SUCCESS, map);
    }

    /**
     * 模糊查询学校部门信息
     *
     * @param args
     * @return
     */
    @Override
    public Result getSchoolDepartmentsByArgs(String args) throws SQLException {
        List<Department> departmentList = schoolDBService.getDepartmentsByArgs(args);
        if (departmentList == null) {
            return Result.failure(ResultCode.FAILURE);
        }
        return Result.success(ResultCode.SUCCESS, departmentList);
    }

    /**
     * 授权部门
     *
     * @param dep_id
     * @return
     */
    @Override
    public Result authDepartment(Integer dep_id, String dep_name) throws SQLException {
        Department department = departmentMapper.selectOne(new QueryWrapper<Department>().eq("dep_id", dep_id));
        if (department == null) {
            int insert = departmentMapper.insert(new Department(dep_id, dep_name, null, null));
            if (insert > 0) {
                numProjectService.addNumProject(dep_id, dep_name);
                redisUtils.del("departmentList");
                return Result.success(ResultCode.SUCCESS);
            } else {
                return Result.failure(ResultCode.FAILURE);
            }
        } else {
            int delete = departmentMapper.delete(new QueryWrapper<Department>().eq("dep_id", dep_id));
            if (delete > 0) {
                numProjectService.deleteNumProject(dep_id);
                redisUtils.del("departmentList");
                return Result.success(ResultCode.SUCCESS);
            } else {
                return Result.failure(ResultCode.FAILURE);
            }
        }
    }

    @Override
    public List<Department> getAuthDepartment() {
        return departmentMapper.selectList(null);
    }

    @Override
    public Result getSimplifyDepartment() throws SQLException {
        return Result.success(ResultCode.SUCCESS, schoolDBService.getSimplifyDepartment());
    }
}
